using CfgTable;
using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;
using UnityEngine;

namespace IQIGame.Onigao.GamePlay
{
    public class UIN_FormationSoulCell : UIBaseNode
    {
        #region Template Generate,don't modify
        protected partial class UIB_UIN_FormationSoulCell
        {
            #region ObjectBinding Generate
            public UnityEngine.GameObject empty { protected set; get; }
            public UnityEngine.GameObject formationInfo { protected set; get; }
            public UnityEngine.GameObject captain { protected set; get; }
            public UnityEngine.GameObject infoRoot { protected set; get; }
            public UnityEngine.GameObject model { protected set; get; }
            public IQIGame.Onigao.Framework.ExArtSpriteNum classTypeIcon { protected set; get; }
            public IQIGame.Onigao.Framework.ExArtSpriteNum attributeTypeIcon { protected set; get; }
            public UnityEngine.GameObject lv { protected set; get; }
            public UnityEngine.GameObject star { protected set; get; }
            public UnityEngine.GameObject fightPower { protected set; get; }
            public UnityEngine.GameObject attrInfoRoot { protected set; get; }
            public UnityEngine.GameObject attMould { protected set; get; }
            public IQIGame.Onigao.Framework.ExText textRoleName { protected set; get; }
            public IQIGame.Onigao.Framework.EventTriggerListener formationSoulCell { protected set; get; }
            public UnityEngine.GameObject replace { protected set; get; }
            public IQIGame.Onigao.Framework.ExButton formationSoulCellBtn { protected set; get; }
            public virtual void InitBinding(ObjectBinding __binding)
            {
                __binding.TryGetVariableValue<UnityEngine.GameObject>("empty", out var __tbv0);
                this.empty = __tbv0;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("formationInfo", out var __tbv1);
                this.formationInfo = __tbv1;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("captain", out var __tbv2);
                this.captain = __tbv2;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("infoRoot", out var __tbv3);
                this.infoRoot = __tbv3;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("model", out var __tbv4);
                this.model = __tbv4;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExArtSpriteNum>("classTypeIcon", out var __tbv5);
                this.classTypeIcon = __tbv5;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExArtSpriteNum>("attributeTypeIcon", out var __tbv6);
                this.attributeTypeIcon = __tbv6;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("lv", out var __tbv7);
                this.lv = __tbv7;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("star", out var __tbv8);
                this.star = __tbv8;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("fightPower", out var __tbv9);
                this.fightPower = __tbv9;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("attrInfoRoot", out var __tbv10);
                this.attrInfoRoot = __tbv10;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("attMould", out var __tbv11);
                this.attMould = __tbv11;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExText>("textRoleName", out var __tbv12);
                this.textRoleName = __tbv12;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.EventTriggerListener>("formationSoulCell", out var __tbv13);
                this.formationSoulCell = __tbv13;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("replace", out var __tbv14);
                this.replace = __tbv14;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExButton>("formationSoulCellBtn", out var __tbv15);
                this.formationSoulCellBtn = __tbv15;
            }
            #endregion ObjectBinding Generate 
        }
        #endregion Template Generate,don't modify

        #region fields & properties
        UIN_CommonTitleValue _lv = null;
        UIN_CommonTitleValue _ftPower = null;
        ListScrollAdapter<UIN_CommonDarkLight> _listStar = null;
        ListScrollAdapter<UIN_CommonAttribute> _listAttr = null;

        FormationHeroData posHereData = null;
        HeroData _heroData = null;
        bool _isHasHero = false;
        bool isStateShowAttr = false;
        bool isSetCaptain = false;
        UIEntityViewHelper entityHelper = null;

        protected UIB_UIN_FormationSoulCell ui { get; set; }
        #endregion

        protected override void BeforeInit()
        {
            ui = new UIB_UIN_FormationSoulCell();
            ui.InitBinding(this.csObjBind);
        }

        protected override void OnInit()
        {
            this._lv = this.InitChildNode<UIN_CommonTitleValue>(this.ui.lv);
            this._ftPower = this.InitChildNode<UIN_CommonTitleValue>(this.ui.fightPower);

            this._listStar = this.InitListScroll<UIN_CommonDarkLight>(this.ui.star, this.OnStarChanged);
            this._listAttr = this.InitListScroll<UIN_CommonAttribute>(this.ui.attMould, this.OnAttrChanged);
            this.ui.replace.SetActive(false);

            this.ui.formationSoulCell.onClick += OnClickSelf;
        }

        protected override void OnShow()
        {

        }

        protected override void OnHide()
        {

        }

        private void OnClickSelf(GameObject go, UnityEngine.EventSystems.PointerEventData eventData)
        {
            if (this.isSetCaptain)
            {
                if (this._isHasHero)
                {

                }
            }
            else
            {
                this.OpenUI<UIFormationPopup>(UD_FormationPopup.Create(posHereData));
            }
        }

        protected override void OnDispose()
        {
            this._heroData = null;
            this.ClearModel();
        }

        public void Show(FormationHeroData data, bool isStateShowAttr, bool isSetCaptain)
        {
            this.Show();
            this.isSetCaptain = isSetCaptain;
            this.isStateShowAttr = isStateShowAttr;
            this.posHereData = data;

            this._isHasHero = data.isHasHero;
            this._heroData = data.heroData;

            this.ui.empty.SetActive(!this._isHasHero);
            this.ui.formationInfo.SetActive(this._isHasHero);

            if (!isSetCaptain)
                this.ui.formationSoulCellBtn.SetSelectState(false);
            this.ui.formationSoulCellBtn.enabled = isSetCaptain;

            this.ui.replace.SetActive(!data.isRegularHeroPos);

            bool isCaptain = data.isCaptain;
            this.ui.captain.SetActive(isCaptain);

            if (this._isHasHero)
                this.ViewormationInfo();
            else
                this.ClearModel();
        }

        void ViewormationInfo()
        {
            bool isAttr = this.isStateShowAttr;

            this.ui.infoRoot.SetActive(!isAttr);
            this.ui.attrInfoRoot.SetActive(isAttr);

            string assetPathModel = this._heroData.cfgSkin.UIEntityAsset_fullPath;
            this.ShowModel(assetPathModel, this.ui.model.transform);

            this.ui.textRoleName.text = HeroModule.Instance.GetHeroName(this._heroData);
            //this.ui.textRoleName.indexSwitchColor = isAttr ? 1 : 0;
            CfgHero cfgHero = this._heroData.cfgData;
            this.ui.classTypeIcon.SetInt(cfgHero.HeroClass);
            this.ui.attributeTypeIcon.SetInt(cfgHero.HeroElementType);

            if (isAttr)
            {
                var attrs = APIUIHeroInfo.DetailAttributeIds;
                this._listAttr.RefillCells(attrs.Length);
            }
            else
            {
                this._lv.Show(this._heroData.level.ToString());
                this._ftPower.Show(this.posHereData.belong.fightPower.ToString());
                this._listStar.RefillCells(APIUIConst.HeroDefautMaxStar);
            }
        }

        private void ShowModel(string assetPath, Transform parent)
        {
            if (this.entityHelper == null)
                this.entityHelper = UIEntityViewHelper.NewFromPool();
            this.entityHelper.ShowModel(assetPath, parent);
        }

        private void ClearModel()
        {
            this.entityHelper?.Put2Pool();
            this.entityHelper = null;
        }

        void OnStarChanged(UIN_CommonDarkLight item, int index)
        {
            int nCurr = index;
            int bigStar = -1;
            if (this._heroData.cfgStar != null)
            {
                bigStar = this._heroData.cfgStar.BigStar - 1;
            }
            bool isLight = (bigStar >= nCurr);
            item.Show(isLight);
        }
        void OnAttrChanged(UIN_CommonAttribute item, int index)
        {
            var attrCid = APIUIHeroInfo.DetailAttributeIds[index];
            var attrVal = this._heroData.GetAttr(attrCid);
            item.Show(attrCid, attrVal);
        }
    }
}

